import webSocket from '@ohos.net.websocket';
export default {
    data: {
        title: 'World',
    },
    request(){
        console.log('进入websocket')
        let ws = webSocket.createWebSocket();
        let url = "ws://82.157.123.54:9010/ajaxchattest"
        ws.connect(url, (err, value) => {
            console.log("connect, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('open', (err, value) => {
            console.log("on open, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value));
        });
        ws.on('message', (err, value) => {
            console.log("on message, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('close', (err, value) => {
            console.log("on close, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('error', (err) => {
            console.log("on error, error:" + JSON.stringify(err))
        });
    },
    requestOption(){
        let ws = webSocket.createWebSocket();
        let url = "ws://82.157.123.54:9010/ajaxchattest"
        ws.connect(url, {
            header: {
                "key": "value",
                "key2": "value2"
            }
        }, (err, value) => {
            console.log("connect, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('open', (err, value) => {
            console.log("on open, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value));
        });
        ws.on('message', (err, value) => {
            console.log("on message, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('close', (err, value) => {
            console.log("on close, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('error', (err) => {
            console.log("on error, error:" + JSON.stringify(err))
        });

        //        promise 方式
        //        let promise = ws.connect(url);
        //        promise.then((value) => {
        //            console.log("connect success, value:" + JSON.stringify(value))
        //        }).catch((err) => {
        //            console.log("connect fail, error:" + JSON.stringify(err))
        //        });
    },
    send(){
        let ws = webSocket.createWebSocket();
        let url = "ws://82.157.123.54:9010/ajaxchattest"
        ws.connect(url, (err, value) => {
            ws.send("Hello, server!", (err, data) => {
                console.log("send, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
            });
        });
//        方式二 promise
//        ws.connect(url, (err, value) => {
//            let promise = ws.send("Hello, server!");
//            promise.then((value) => {
//                console.log("send success, value:" + JSON.stringify(value))
//            }).catch((err) => {
//                console.log("send fail, error:" + JSON.stringify(err))
//            });
//        });
        ws.on('open', (err, value) => {
            console.log("on open, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value));
        });
        ws.on('message', (err, value) => {
            console.log("on message, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('close', (err, value) => {
            console.log("on close, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('error', (err) => {
            console.log("on error, error:" + JSON.stringify(err))
        });
    },
    close(){
        let ws = webSocket.createWebSocket();
        let url = "ws://82.157.123.54:9010/ajaxchattest"
        ws.connect(url, (err, value) => {
            console.log("connect, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
        });
        ws.on('message', (err, value) => {
            console.log("on message, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
            if (value === 'bye') {
                ws.close((err, value) => {
                    console.log("close, error:" + JSON.stringify(err))
                });
            }
        });

//        方式二 promise
//        ws.on('message', (err, value) => {
//            console.log("on message, error:" + JSON.stringify(err) + ",value:" + JSON.stringify(value))
//            if (value === 'value1') {
//                let promise = ws.close({
//                    code: 404,
//                    reason: "value2"
//                });
//                promise.then((value) => {
//                    console.log("close success, value:" + JSON.stringify(value))
//                }).catch((err) => {
//                    console.log("close fail, error:" + JSON.stringify(err))
//                });
//            }
//        });
    }
}
  